package com.hunanwenli.controller.login.register;

import com.hunanwenli.tool.Tools;
import com.hunanwenli.service.login.FeignLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;



/**
 * @author wanzhenyu
 * @date 2021/5/30 17:02
 */
@Controller
public class registerController {

    @Autowired
    private FeignLoginService feignLoginService;

    @RequestMapping(value = "/register")
    public String register(Model model, HttpServletRequest request){
        String token = Tools.makeToken();
        model.addAttribute("token",token);
        model.addAttribute("txt","saddddddddddddddddddddddddddddddddddddddddddddddddddd");
        request.getSession().setAttribute("token",token);
        return "registered/register";

    }

    @RequestMapping(value = "/register2",method = RequestMethod.POST)
    public String  register2(Model model,String token,String email,HttpServletRequest request){
        if (Tools.isNullOrEmpty(token)) {
            return "forward:/register";}
        if (Tools.isNullOrEmpty(email)){
            return "forward:/register";
        }
        boolean isToken = token.equals(request.getSession().getAttribute("token"));
        if (isToken){
            request.getSession().removeAttribute("token");
            token = Tools.makeToken();
            model.addAttribute("token",token);
            model.addAttribute("email",email);
            request.getSession().setAttribute("token",token);
        }else {
            return "forward:/register";
        }
        return "registered/register2";
    }


    @RequestMapping(value = "/fillPwd",method = RequestMethod.POST)
    public String fillPwd(Model model,String email,String token,String phone,String name,String pwd,HttpServletRequest request){
        if (Tools.isNullOrEmpty(token)) {
            return "forward:/register";}
        if (Tools.isNullOrEmpty(email)||Tools.isNullOrEmpty(phone)||Tools.isNullOrEmpty(name)||Tools.isNullOrEmpty(pwd)){
            return "forward:/register";
        }
        boolean isToken = token.equals(request.getSession().getAttribute("token"));
        if (isToken){
            request.getSession().removeAttribute("token");
            token = Tools.makeToken();
            model.addAttribute("token",token);
        }else {
            return "forward:/register";
        }
        request.getSession().setAttribute("token",token);
        model.addAttribute("phone",phone);
        boolean isInsert = feignLoginService.fillPwd(email, name, phone, pwd);
        if (!isInsert){
            token = Tools.makeToken();
            model.addAttribute("token",token);
            model.addAttribute("email",email);
            model.addAttribute("error","账号也被注册");
            return "registered/register2";

        }
        model.addAttribute("password",pwd);
        model.addAttribute("phone",phone);
        return "registered/register3";
    }

    @RequestMapping(value = "/ajaxEmail",method = RequestMethod.POST)
    @ResponseBody
    public String ajaxEmail(String email){
       return feignLoginService.ajaxEmail(email);
    }

    @RequestMapping(value = "/ajaxYanZhenMa",method = RequestMethod.POST)
    @ResponseBody
    public String ajaxYanZhenMa(String code,String email){
        return feignLoginService.ajaxYanZhenMa(code,email);
    }

    @RequestMapping(value = "/ajaxYanZhenMa2",method = RequestMethod.POST)
    @ResponseBody
    public String ajaxYanZhenMa2(String code,String codeval){
        return feignLoginService.ajaxYanZhenMa2(code,codeval);
    }

    @RequestMapping(value = "/ajaxPhone",method = RequestMethod.POST)
    @ResponseBody
    public String ajaxPhone(String phone){
        return feignLoginService.ajaxPhone(phone);
    }

}
